package ru.ifmo.dao;

import org.hibernate.Query;
import org.hibernate.Session;
import ru.ifmo.HibernateUtil;
import ru.ifmo.LocationSelectBean;
import ru.ifmo.entities.FlightsEntity;

import java.util.Collections;
import java.util.List;

public class GeoPlacesDAO {
    private LocationSelectBean fromBean;
    private LocationSelectBean toBean;

    public LocationSelectBean getFromBean() {
        return fromBean;
    }

    public void setFromBean(LocationSelectBean fromBean) {
        this.fromBean = fromBean;
    }

    public LocationSelectBean getToBean() {
        return toBean;
    }

    public void setToBean(LocationSelectBean toBean) {
        this.toBean = toBean;
    }

    Session session = HibernateUtil.getSession();

    public List<FlightsEntity> getFlights() {
        List<FlightsEntity> flightsList;
        try {
            Query flightsQuery = null;
            if (((fromBean != null) && (toBean != null)) && ((fromBean.getCity() != null) && (toBean.getCity() != null))) {
                flightsQuery = session.createQuery("from FlightsEntity where (cityFrom=:cityFrom) AND (cityTo = :cityTo)");
            } else {
                flightsQuery = session.createQuery("from FlightsEntity");
            }
            flightsList = flightsQuery.list();
        } catch (Exception e) {
            flightsList = Collections.emptyList();
            e.printStackTrace();
        }
        return flightsList;
    }

    public List<String> getCountriesNames() {
        List<String> countriesNames;
        try {
            countriesNames = session.createQuery("select title from CountriesEntity ").list();
        } catch (Exception e) {
            countriesNames = Collections.emptyList();
            e.printStackTrace();
        }
        return Collections.unmodifiableList(countriesNames);
    }

    public String getCityNameById(int id) {
        /* int cityId = flightRow.getFlightId();
        Query query = session.getNamedQuery("city.findById");
       query.setInteger("id", cityId);
       return String.valueOf(query.uniqueResult());*/
        return "aa";
    }

}